//
//  CDTabbarVC.m
//  CustomTabbar
//
//  Created by Dong Chen on 2017/8/31.
//  Copyright © 2017年 Dong Chen. All rights reserved.
//

#import "CDTabbarVC.h"
#import "CDNavigationVC.h"
#import "CDTabBar.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DistributeViewController.h"
#import "ReportViewController.h"
#import "FindViewController.h"
#import "MyViewController.h"


@interface CDTabbarVC ()<UITabBarControllerDelegate>
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, strong) MessageViewController *messageVC;
@property (nonatomic, strong) ReportViewController *reportVC;
@property (nonatomic, strong) FindViewController *findVC;
@property (nonatomic, strong) MyViewController *myVC;

@property (nonatomic,strong)NSMutableArray *viewCtrlsArray;

@end

@implementation CDTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [UITabBar appearance].backgroundColor = [UIColor whiteColor];
    [UITabBar appearance].translucent = NO;
    [self InitView];
    [self InitMiddleView];
}

- (void)InitMiddleView
{
    CDTabBar *tabBar = [[CDTabBar alloc] init];
//    [self setValue:tabBar forKey:@"tabBar"];
//    @WeakObj(tabBar);
    [tabBar setDidMiddBtn:^{
        
//        ReportViewController *reportVc = [[ReportViewController alloc] init];
//        CDNavigationVC *nav = [[CDNavigationVC alloc] initWithRootViewController:reportVc];
//
//        [self presentViewController:nav  animated:YES completion:nil];
    }];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@" --- %@", item.title);
   
}

- (void)InitView
{
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray *titles = @[@"首页", @"消息", @"", @"发现", @"我的"];
    NSArray *images = @[@"home", @"mark", @"open_icon_huise",@"discover", @"people"];
    NSArray *selectedImages = @[@"home_fill", @"mark_fill",@"open_icon", @"discover_fill", @"people_fill"];
    HomeViewController * homeVc = [[HomeViewController alloc] init];
    self.homeVC = homeVc;
    MessageViewController * messageVc = [[MessageViewController alloc] init];
    self.messageVC = messageVc;
    ReportViewController * reportVC = [[ReportViewController alloc] init];
    self.reportVC = reportVC;
    FindViewController * findVC = [[FindViewController alloc] init];
    self.findVC = findVC;
    MyViewController * myVC = [[MyViewController alloc] init];
    self.myVC = myVC;
    NSArray *viewControllers = @[homeVc, messageVc,reportVC, findVC, myVC];
    for (int i = 0; i < viewControllers.count; i++)
    {
        UIViewController *childVc = viewControllers[i];
        [self setVC:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i] index:i];
        
//        [self setVC:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i]];
    }
}

- (void)setVC:(UIViewController *)VC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage index:(NSInteger)index
{
    VC.title = title;
//    VC.tabBarItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor blackColor];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [VC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    VC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    VC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if (index == 2) {
        
          VC.tabBarItem.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0);
    }
  
    CDNavigationVC *nav = [[CDNavigationVC alloc] initWithRootViewController:VC];
    [self addChildViewController:nav];
}

- (void)initCtrls {
    
}


- (NSMutableArray *)viewCtrlsArray{
    
    if (_viewCtrlsArray) {
        _viewCtrlsArray = [[NSMutableArray alloc] init];
    }
    return _viewCtrlsArray;
}

@end
